<?php
declare(strict_types=1);

namespace App\Controller\Backend;

use App\Logic\AdminMenuLogic;
use App\Utils\Tree;
use Exception;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Http\Message\ResponseInterface;

#[AutoController(prefix: '/menu', server: 'backend')]
class MenuController extends AbstractController
{
    /**
     * 菜单列表
     * @return ResponseInterface
     */
    public function lists(): ResponseInterface
    {
        return $this->apiReturn(200, 'succ', array_values(AdminMenuLogic::lists()));
    }

    /**
     * 菜单详情
     * @return ResponseInterface
     */
    public function detail(): ResponseInterface
    {
        $id = (int)$this->request->input('id');
        if (!$id) {
            return $this->apiReturn(400, '参数错误');
        }
        $detail = AdminMenuLogic::detail($id);
        $menus = AdminMenuLogic::lists();
        if ($detail['parentid'] && isset($menus[$detail['parentid']])) {
            $menus[$detail['parentid']]['selected'] = ' selected="selected"';
        }
        return $this->apiReturn(200, 'succ', [
            'detail' => AdminMenuLogic::detail($id),
            'options' => $this->_getTreeOptions($menus, $detail['parentid'])
        ]);
    }

    /**
     * 修改菜单
     * @return ResponseInterface
     */
    public function edit(): ResponseInterface
    {
        $id = (int)$this->request->input('id', 0);
        if (!$id) {
            return $this->apiReturn(400, '参数错误');
        }
        $input = $this->request->all();
        try {
            AdminMenuLogic::edit($id, $input);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 添加菜单
     * @return ResponseInterface
     */
    public function add(): ResponseInterface
    {
        $input = $this->request->all();
        try {
            AdminMenuLogic::add($input);
            return $this->apiReturn(200, '添加成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 删除菜单
     * @return ResponseInterface
     */
    public function delete(): ResponseInterface
    {
        $id = (int)$this->request->input('id', 0);
        if (!$id) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            AdminMenuLogic::delete($id);
            return $this->apiReturn(200, '删除成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 获取父栏目options
     * @return ResponseInterface
     */
    public function getTreeOptions(): ResponseInterface
    {
        $parentId = (int)$this->request->input('parentId', 0);
        $menus = AdminMenuLogic::lists();
        if ($parentId && isset($menus[$parentId])) {
            $menus[$parentId]['selected'] = ' selected="selected"';
        }
        $options = $this->_getTreeOptions($menus, $parentId);
        return $this->apiReturn(200, 'succ', ['options' => $options]);
    }

    /**
     * 快速修改
     * @return ResponseInterface
     */
    public function updateField(): ResponseInterface
    {
        $input = $this->request->all();
        if (!intval($input['id']) || empty($input['field']) || !isset($input['value'])) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            AdminMenuLogic::updateField($input['id'], $input['field'], $input['value']);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, '修改失败');
        }
    }

    private function _getTreeOptions($menu, $parentId = 0): string
    {
        $tree = new Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $str = '<option value=\"$id\" $selected>$spacer $name $display_icon</option>';
        $tree->init($menu);
        return $tree->get_tree(0, $str, $parentId);
    }
}